package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.fragment.plan.PlanFragment1;
import com.wnhz.work.yz.fragment.plan.PlanFragment2;
import com.wnhz.work.yz.fragment.plan.PlanFragment3;
import com.wnhz.work.yz.fragment.plan.PlanFragment4;

import java.util.ArrayList;
import java.util.List;

public class PlansActivity extends BaseActivity implements View.OnClickListener {


    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private PlanFragment1 fragment1;
    private PlanFragment2 fragment2;
    private PlanFragment3 fragment3;
    private PlanFragment4 fragment4;
  //  private PlanFragment5 fragment5;
    private TextView title1, title2, title3, title4;
            //, title5;
    private View line1, line2, line3, line4;
    //, line5;
    private int textColorNor, textColorCli;
    private List<View> lines = new ArrayList<>();
    private List<TextView> titles = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plans);
        initTitle();
        initView();

    }

    private void initView() {
        findViewById(R.id.shop_title1).setOnClickListener(this);
        findViewById(R.id.shop_title2).setOnClickListener(this);
        findViewById(R.id.shop_title3).setOnClickListener(this);
        findViewById(R.id.shop_title4).setOnClickListener(this);
        findViewById(R.id.shop_title5).setOnClickListener(this);
        title1 = (TextView) findViewById(R.id.shop_title1);
        title2 = (TextView) findViewById(R.id.shop_title2);
        title3 = (TextView) findViewById(R.id.shop_title3);
        title4 = (TextView) findViewById(R.id.shop_title4);
    //    title5 = (TextView) findViewById(R.id.shop_title5);
        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);
     //   line5 = findViewById(R.id.line5);
        fragmentList = new ArrayList<>();
        fragment1 = new PlanFragment1();
        fragment2 = new PlanFragment2();
        fragment3 = new PlanFragment3();
        fragment4 = new PlanFragment4();
        //fragment5 = new PlanFragment5();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
      //  fragmentList.add(fragment5);
        lines.add(line1);
        lines.add(line2);
        lines.add(line3);
        lines.add(line4);
      //  lines.add(line5);
        titles.add(title1);
        titles.add(title2);
        titles.add(title3);
        titles.add(title4);
       // titles.add(title5);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_shopping_card, fragment1);
        transaction.add(R.id.fl_shopping_card, fragment2);
        transaction.add(R.id.fl_shopping_card, fragment3);
        transaction.add(R.id.fl_shopping_card, fragment4);
      //  transaction.add(R.id.fl_shopping_card, fragment5);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
      //  transaction.hide(fragment5);
        transaction.commit();

    }

    private void initTitle() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);

        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("新增");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("计划");
        title.setTextColor(textColorCli);
        //title.setTextSize(23);
        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);

        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.shop_title1:
                hideFragment(0);
                break;
            case R.id.shop_title2:
                hideFragment(1);
                break;
            case R.id.shop_title3:
                hideFragment(2);
                break;
            case R.id.shop_title4:
                hideFragment(3);
                break;
            case R.id.shop_title5:
                hideFragment(4);
                break;
            case R.id.submit:
                startActivity(new Intent(this,AddPlanActivity.class));
                break;

        }

    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
                titles.get(i).setTextColor(textColorCli);
                lines.get(i).setVisibility(View.VISIBLE);
            } else {
                transaction.hide(fragmentList.get(i));
                titles.get(i).setTextColor(textColorNor);
                lines.get(i).setVisibility(View.INVISIBLE);
            }

        }
        transaction.commit();
    }
}
